﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using NetBpm.Util.Client;
using NetBpm.Workflow.Organisation.EComp;
using NetBpm.Workflow.Organisation;
using NetBpm.Workflow.Delegation;
using NetBpm.Workflow.Definition;
using NetBpm.Workflow.Definition.EComp;
using NetBpm.Workflow.Execution;
using NetBpm.Workflow.Execution.EComp;
using NetBpm.Workflow.Definition.Attr;
using NetBpm.Workflow.Log.EComp;

using BEGDC.Utility.Castle;

namespace BEGDC.CC.Web
{
	/// <summary>
	/// Summary description for ProcessManagement.
	/// </summary>
	public class ProcessManagement : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Button Deploy;
		protected System.Web.UI.HtmlControls.HtmlInputFile File1;
		protected System.Web.UI.WebControls.Button Button1;
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if (!this.IsPostBack)
			{
				this.LoadProcessDefination();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Deploy.Click += new System.EventHandler(this.Deploy_Click);
			this.DataGrid1.ItemCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCommand);
			this.Button1.Click += new System.EventHandler(this.Button1_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void LoadProcessDefination()
		{
			IDefinitionSessionLocal definitionComponent = null;
			try
			{
				definitionComponent = AppContainer.Instance["DefinitionSession"] as IDefinitionSessionLocal;
				IList definations = definitionComponent.GetProcessDefinitions();
				this.DataGrid1.DataSource = definations;
				this.DataGrid1.DataBind();				
			} 
			catch(Exception ex)
			{
				throw new Exception (ex.Message);
			}
			finally
			{
				ServiceLocator.Instance.Release(definitionComponent);
			}
		}

		private void Deploy_Click(object sender, System.EventArgs e)
		{
			if (this.File1.PostedFile.InputStream.Length == 0)
				return;
			byte[] buff = new byte[this.File1.PostedFile.InputStream.Length];
			this.File1.PostedFile.InputStream.Read(buff, 0, buff.Length);

			IDefinitionSessionLocal definitionComponent = null;
			try
			{
				definitionComponent = AppContainer.Instance["DefinitionSession"] as IDefinitionSessionLocal;
				definitionComponent.DeployProcessArchive(buff);
				
			} 
			catch(Exception ex)
			{
				throw new Exception (ex.Message);
			}
			finally
			{
				ServiceLocator.Instance.Release(definitionComponent);
			}
			this.LoadProcessDefination();
		}

		private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
		{
			if(e.CommandName=="Start")
			{
				string id = e.Item.Cells[0].Text;
				Response.Redirect("ProcessExecute.aspx?processDefinitionId="+id);
			}
			if(e.CommandName=="Delete")
			{
				long id = long.Parse(e.Item.Cells[0].Text);
				DeleteProcess(id);
				//Get processes
				LoadProcessDefination();			
			}
			if(e.CommandName=="Show")
			{
				long id = long.Parse(e.Item.Cells[0].Text);
				ShowProcess(id);
				//Get processes
				LoadProcessDefination();			
			}
		}

		private void DeleteProcess(long processID)
		{
			IDefinitionSessionLocal definitionComponent = null;
			try
			{
				definitionComponent = AppContainer.Instance["DefinitionSession"] as IDefinitionSessionLocal;
				definitionComponent.DeleteProcessDefinition(processID);
			} 
			catch(Exception ex)
			{
				throw new Exception (ex.Message);
			}
			finally
			{
				ServiceLocator.Instance.Release(definitionComponent);
			}
		}

		private void ShowProcess(long processID)
		{
			IDefinitionSessionLocal definitionComponent = null;
			try
			{
				definitionComponent = AppContainer.Instance["DefinitionSession"] as IDefinitionSessionLocal;
				IProcessDefinition def =  definitionComponent.GetProcessDefinition(processID);
			
				IEnumerator iter = def.Nodes.GetEnumerator();
				while (iter.MoveNext())
				{
					INode node = (INode)iter;
					System.Diagnostics.Debug.WriteLine(node.Name);
					//					NetBpm.Workflow.Log.ILogDetail detail = (NetBpm.Workflow.Log.ILogDetail)iter.Current;
					//					if (detail is NetBpm.Workflow.Log.IObjectReference)
					//					{
					//						previous_node_id = ((NetBpm.Workflow.Log.IObjectReference)detail).ReferenceId;
					//						break;
					//					}
				}
			} 
			catch(Exception ex)
			{
				throw new Exception (ex.Message);
			}
			finally
			{
				ServiceLocator.Instance.Release(definitionComponent);
			}
		}

		private void Button1_Click(object sender, System.EventArgs e)
		{
			BEGDC.CC.BL.Workflow.IDefinition def = BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.CC.BL.Common.ServiceConst.DefinitionService] as BEGDC.CC.BL.Workflow.IDefinition;
			
		}
	}
}
